#include <stdlib.h>
#include <stdio.h>
#include <typecomm.h>
#include <logbase.h>
#include <systime.h>
#include <prmOpt.h>
#include <fileOpt.h>

struct CheckPrmStc
{
    int32_t pI32Val1;
    int64_t pI64Val1;
    float pFltVal1;
    double pDblVal1;
    char pStr1[128];
    int32_t pI32Buf1[2];
    char pStrBuf1[2][128];
};

struct prmMgrBaseStc myTestPrmMgr;
struct CheckPrmStc myTestPrmIst;

#define PRM_MGR_TEST_SAVE_PATH "/tmp/prmTestSave.txt"
#define PRM_MGR_TEST_LOCK_PATH "/tmp/prmTestLock.txt"
#define PRM_MGR_TEST_SWAP_PATH "/tmp/prmTestSwap.txt"
int main(int argc, char **argv)
{
    foptCreateIfNotExist(PRM_MGR_TEST_SAVE_PATH);
    
    prmMgrInit(&myTestPrmMgr, PRM_MGR_TEST_SAVE_PATH, PRM_MGR_TEST_SWAP_PATH, PRM_MGR_TEST_LOCK_PATH);
    prmMgrRegI32(&myTestPrmMgr, "pI32Val1", &myTestPrmIst.pI32Val1, 0, PRM_INF_CFG_LEV_8);
    prmMgrRegI64(&myTestPrmMgr, "pI64Val1", &myTestPrmIst.pI64Val1, -1, PRM_INF_CFG_LEV_8);
    prmMgrRegFloat(&myTestPrmMgr, "pFltVal1", &myTestPrmIst.pFltVal1, -2.1, PRM_INF_CFG_LEV_8);
    prmMgrRegDouble(&myTestPrmMgr, "pDblVal1", &myTestPrmIst.pDblVal1, 1.3, PRM_INF_CFG_LEV_8);
    prmMgrRegStr(&myTestPrmMgr, "pStr1", myTestPrmIst.pStr1, sizeof(myTestPrmIst.pStr1), "str0", PRM_INF_CFG_LEV_8);
    prmMgrRegI32(&myTestPrmMgr, "pI32Buf1_0", &myTestPrmIst.pI32Buf1[0], 3, PRM_INF_CFG_LEV_8);
    prmMgrRegI32(&myTestPrmMgr, "pI32Buf1_1", &myTestPrmIst.pI32Buf1[1], 4, PRM_INF_CFG_LEV_8);
    prmMgrRegStr(&myTestPrmMgr, "pStrBuf1_0", myTestPrmIst.pStrBuf1[0], sizeof(myTestPrmIst.pStrBuf1[0]), "strb0", PRM_INF_CFG_LEV_8);
    prmMgrRegStr(&myTestPrmMgr, "pStrBuf1_1", myTestPrmIst.pStrBuf1[1], sizeof(myTestPrmIst.pStrBuf1[1]), "strb1", PRM_INF_CFG_LEV_8);
    // add pram before finish!
    prmMgrRegFinish(&myTestPrmMgr);

    logdebugc("reset check");
    prmMgrReset(&myTestPrmMgr, 0);
    prmMgrPrintVals(&myTestPrmMgr);

    logdebugc("sync from file check");
    prmMgrReadFile(&myTestPrmMgr);
    prmMgrPrintVals(&myTestPrmMgr);

    myTestPrmIst.pI32Val1 = 10;
    myTestPrmIst.pI64Val1 = 11;
    // myTestPrmIst.pFltVal1 = 12;
    myTestPrmIst.pDblVal1 = 13;
    snprintf(myTestPrmIst.pStr1, sizeof(myTestPrmIst.pStr1), "str10");
    myTestPrmIst.pI32Buf1[0] = 20;
    // myTestPrmIst.pI32Buf1[1] = 21;
    // snprintf(myTestPrmIst.pStrBuf1[0], sizeof(myTestPrmIst.pStrBuf1[0]), "strb10");
    snprintf(myTestPrmIst.pStrBuf1[1], sizeof(myTestPrmIst.pStrBuf1[1]), "strb11");
    logdebugc("change check");
    prmMgrPrintVals(&myTestPrmMgr);

    prmMgrSaveFile(&myTestPrmMgr);

    logdebugc("free check");
    prmMgrFree(&myTestPrmMgr);
    return 0;
}